package sf01.studentmngmsystem.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import jakarta.annotation.PostConstruct;
import java.io.File;

@Configuration
@ConfigurationProperties(prefix = "file.upload")
public class FileUploadConfig implements WebMvcConfigurer {
    
    private String path = "uploads/";
    private long maxSize = 10 * 1024 * 1024; // 10MB
    private String[] allowedTypes = {"jpg", "jpeg", "png", "gif", "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "zip", "rar"};
    
    @PostConstruct
    public void init() {
        // 确保上传目录存在
        File uploadDir = new File(path);
        if (!uploadDir.exists()) {
            boolean created = uploadDir.mkdirs();
            if (created) {
                System.out.println("创建上传目录：" + path);
            } else {
                System.err.println("无法创建上传目录：" + path);
            }
        }
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + path);
    }
    
    // getters and setters
    public String getPath() { 
        return path; 
    }
    
    public void setPath(String path) { 
        this.path = path; 
    }
    
    public long getMaxSize() { 
        return maxSize; 
    }
    
    public void setMaxSize(long maxSize) { 
        this.maxSize = maxSize; 
    }
    
    public String[] getAllowedTypes() { 
        return allowedTypes; 
    }
    
    public void setAllowedTypes(String[] allowedTypes) { 
        this.allowedTypes = allowedTypes; 
    }
}